<template>
 <div class="my-body">
    <el-row>
      <el-col :span="24">
        <div class="my-table-search">
          <el-form ref="form" :model="form">
            <el-row :gutter="10">
              <el-col :span="3">
                <el-input v-model="input" placeholder="请输入内容" />
              </el-col>
             
              <el-col :span="3">
                <el-button type="primary" plain>查询</el-button>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="my-table-tools">
          <div class="my-table-tools-left">
            <el-button type="primary" size="small" icon="el-icon-plus" plain @click="handleAddRole">新增</el-button>
            <el-button type="danger" size="small" icon="el-icon-delete" plain>删除</el-button>
          </div>
          <div class="my-table-tools-right">
            <el-button icon="el-icon-refresh" circle plain @click="refresh" />
          </div>
        </div>
      </el-col>
      <el-col :span="24">
        <div class="my-table-body">
 
    <el-table :data="roles" border  style="width: 100%" :header-cell-style="{background:'#f5f7fa',color:'#606266'}" height="620">
      <el-table-column prop="id" label="ID" ></el-table-column>
      <el-table-column
        prop="name"
        label="角色名称"
       
      ></el-table-column>
      <el-table-column prop="weigh" label="排序"></el-table-column>

      <el-table-column
        prop="create_time"
        label="创建时间"
      
      ></el-table-column>
      <el-table-column prop="remark" label="备注" ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleEdit(scope.row)">
            <i class="el-icon-edit"></i>编辑</el-button
          >
          <el-button
          type="text" 
            size="mini"
            @click="handleAddRolefenpei(scope.row)"
          >
            <i class="el-icon-setting"></i> 分配权限</el-button
          >
          <el-button size="mini" type="text"  class="my-btn-text-color-2" @click="handleDelete(scope.row)"
            ><i class="el-icon-delete"></i>删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="my-table-pagination">
          <el-pagination
            background
            :current-page="currentPage4"
            :page-sizes="[100, 200, 300, 400]"
            :page-size="100"
            layout="->, total, sizes, prev, pager, next, jumper"
            :total="400"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-col>
    </el-row>
    <el-dialog title="添加角色" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="newRole" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="newRole.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="newRole.weigh"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="newRole.remark"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="addRole()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- <el-dialog title="新增分配权限" :visible.sync="dialogVisiblefenpeitj" width="30%">
      <div>菜单管理</div>
      <div>
        <el-tree
          :data="data"
          show-checkbox
          node-key="id"
          :props="defaultProps"
          ref="tree"
        >
        </el-tree>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblefenpei = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisiblefenpei = false"
          >确 定</el-button
        >
      </span>
    </el-dialog> -->
    <el-dialog title="分配权限" :visible.sync="dialogVisiblefenpei" width="30%">
      <div>菜单管理</div>
      <div>
        <el-tree
          :data="data"
          show-checkbox
          node-key="id"
          :default-checked-keys="numberArray"
          :props="defaultProps"
          @check-change="handleCheckChange"
          ref="tree"
          :default-expand-all="true"
        >
        </el-tree>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="fenpeiquxiao">取 消</el-button>
        <el-button type="primary" @click="fengpeiqx()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑角色" :visible.sync="dialogVisiblebj" width="30%">
      <el-form ref="form" :model="newRole" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="newRoles.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="newRoles.weigh"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="newRoles.remark"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiaobianji">取 消</el-button>
        <el-button type="primary" @click="addRolebj">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
   
  <script>
import { getData, postData } from "@/utils/request.js";
export default {
  data() {
    return {
      roles: [],
      newRole: { name: "", weigh: "", remark: "" },
      newRoles: { id: "", name: "", weigh: "", remark: "" },
      dialogVisible: false,
      dialogVisiblefenpei: false,
      dialogVisiblebj: false,
      dialogVisiblefenpeitj: false,
      data: [],
      defaultProps: {
        children: "children",
        label: "title", // 这里的 label 对应节点显示的名称
      },
      defaultCheckedKeys: "",
      list: [],
      loading: false
    };
  },
  created() {
    this.jueselist();
    this.caidanlist();
  },
  computed: {
    numberArray() {
      return this.defaultCheckedKeys.split(",").map(Number);
    },
  },
  methods: {
    refresh() {
      const t = this
      t.loading = true
      setTimeout(function() {
        t.loading = false
      }, 2000)
    },
    handleCheckChange(data, checked, indeterminate) {
        console.log(data.id,checked,indeterminate)
      if (indeterminate == true && checked== false  ) {
        let index = this.list.forEach((item) => {
            if (item == data.id) {
              return true;
            }
          });
          this.list.splice(index, 1);
        
      }else if (indeterminate ==false && checked== true  ){
       
          this.list.push(data.id);
       
      } else if(indeterminate ==false && checked== false  ){
        let index = this.list.findIndex((item) => {
            if (item == data.id) {
              return true;
            }
          });
          this.list.splice(index, 1);
          var nameid = this.list.filter(
          (item, index, self) => self.indexOf(item) == index
        );
      }
       else {
      
      }

     
    },
    handleNodeClick(data) {
      console.log(data);
    },

    jueselist() {
      var data = {
        page: 1,
        limit: 100,
        token: localStorage.getItem("token"),
      };
      postData("admin.group/indexList", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.roles = res.data.data;
        } else {
        }
      });
    },
    caidanlist() {
      var data = {
        page: 1,
        like: this.inputlist,
        limit: 100,
        token: localStorage.getItem("token"),
      };
      postData("AuthUrl/indexList", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.data = res.data;
        } else {
        }
      });
    },
    getCheckedNodes() {
      console.log(this.$refs.tree.getCheckedNodes());
    },
    getCheckedKeys() {
      console.log(this.$refs.tree.getCheckedKeys());
    },
    setCheckedNodes() {
      this.$refs.tree.setCheckedNodes([
        {
          id: 5,
          label: "二级 2-1",
        },
        {
          id: 9,
          label: "三级 1-1-1",
        },
      ]);
    },
    setCheckedKeys() {
      this.$refs.tree.setCheckedKeys([3]);
    },
    resetChecked() {
      this.$refs.tree.setCheckedKeys([]);
    },
    handleAddRole() {
      this.dialogVisible = true;
    },
    handleAddRolefenpei(row) {
      console.log(row);
      this.newRoles.id = row.id;
      this.newRoles.name = row.name;
      this.newRoles.weigh = row.weigh;
      this.newRoles.remark = row.remark;
      this.caidanlist();
      this.defaultCheckedKeys = "";
      console.log(this.defaultCheckedKeys, 66666666);
      if (this.defaultCheckedKeys == "") {
        if (row.purview == null) {
        } else {
          this.defaultCheckedKeys = row.purview;
          this.list = this.defaultCheckedKeys.split(",").map(Number);
        }
      } else {
        this.defaultCheckedKeys = "";
      }

      this.dialogVisiblefenpei = true;

      //   this.newRole = { name: "" };
    },
    fengpeiqx() {
      var data = {
        id: this.newRoles.id,
        name: this.newRoles.name,
        weigh: this.newRoles.weigh,
        remark: this.newRoles.remark,
        purview: this.list.join(", "),
        token: localStorage.getItem("token"),
      };
      postData("admin.group/operation", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.jueselist();
          this.$message({
            message: "分配成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦，这是一条错误消息");
        }
      });
      this.dialogVisiblefenpei = false;
    },
    fenpeiquxiao() {
      this.$message.error("取消");
      this.dialogVisiblefenpei = false;
    },
    quxiao() {
      this.$message("已取消");

      this.dialogVisible = false;
    },
    addRole() {
      var data = {
        name: this.newRole.name,
        weigh: this.newRole.weigh,
        remark: this.newRole.remark,
        token: localStorage.getItem("token"),
      };
      postData("admin.group/operation", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.jueselist();
          this.$message({
            message: "添加成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦，这是一条错误消息");
        }
      });
      this.dialogVisible = false;
    },
    quxiaobianji() {
      this.dialogVisiblebj = false;
      this.$message("已取消");
    },
    addRolebj() {
      var data = {
        id: this.newRoles.id,
        name: this.newRoles.name,
        weigh: this.newRoles.weigh,
        remark: this.newRoles.remark,
        token: localStorage.getItem("token"),
      };
      postData("admin.group/operation", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.jueselist();
          this.$message({
            message: "编辑成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦，这是一条错误消息");
        }
      });
      this.dialogVisiblebj = false;
    },
    handleEdit(row) {
      // 编辑操作
      this.newRoles.id = row.id;
      this.newRoles.name = row.name;
      this.newRoles.weigh = row.weigh;
      this.newRoles.remark = row.remark;
      this.dialogVisiblebj = true;
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var data = {
            ids: row.id,
            token: localStorage.getItem("token"),
          };
          postData("admin.group/del", data).then((res) => {
            // 处理响应数据
            if (res.code == 0) {
              console.log(res);
              this.jueselist();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });

      // 删除操作
    },
  },
};
</script>
<style  scoped>

.el-table{
		height: calc(100vh - 282px);
}
</style>